/**
 *
 */
package com.legrand.security.token;

import io.jsonwebtoken.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.authentication.BadCredentialsException;

import java.security.Key;

/**
*@author xiah
*2018-07-10 05:54:51
*/
public class RawAccessJwtToken implements JwtToken {

       private final Logger logger = LoggerFactory.getLogger(getClass());

       private String token;

       public RawAccessJwtToken(String token) {
              this.token = token;
       }

       public Jws<Claims> parseClaims(Key signingKey) {
              try {
                     logger.debug("====================== token: {}", token);
                     logger.debug("====================== signingKey: {}", signingKey);
                     return Jwts.parser().setSigningKey(signingKey).parseClaimsJws(this.token);
              } catch ( UnsupportedJwtException | MalformedJwtException | IllegalArgumentException |
                      SignatureException ex ) {
                     logger.error("Invalid JWT Token", ex);
                     throw new BadCredentialsException("Invalid JWT token: ", ex);
              }
       }

       @Override
       public String getToken() {
              return token;
       }

}
